const {
	src,
	dest,
	series,
	parallel,
	watch
} = require('gulp');		//注意：此文件名要为gulpfile.js，否则会报错没有gulp文件
const dateFormat=require('date-format');
const moment=require('moment');

const template=require('art-template');
// 注册日期过滤器
template.defaults.imports.dateFormat = dateFormat;

// 删除文件		npm i del
const del = require('del');

// 引入 ES6 JS语法转换插件		npm install --save-dev gulp-babel @babel/core @babel/preset-env
const babel = require('gulp-babel');

// 压缩 JS 			npm install --save-dev gulp-uglify
const uglify = require('gulp-uglify');

// 压缩 HTML			npm install --save gulp-htmlmin
const htmlmin = require('gulp-htmlmin');

// 将 less 语法转换成 CSS			npm install gulp-less
const less = require('gulp-less');

// 将 scss 语法转换成 CSS			npm install sass gulp-sass --save-dev
var sass = require('gulp-sass')(require('sass'));

// 压缩 css			npm install gulp-csso --save-dev
const csso = require('gulp-csso');

// nodemon 更新代码实时更新网页		npm install -g nodemon
// gulp-connect 更新代码服务实时更新（可以页面重载和启动一系列服务，如:压缩代码，语法转换等）		npm install --save-dev gulp-connect
const connect = require('gulp-connect');

// 公共头部尾部合并文件		npm install --save-dev gulp-file-include
const fileinclude = require('gulp-file-include');

const configPath={
	commonPath:'./public/common/',
	htmlPath:'./public/',
	cssPath:'./public/css/',
	jsPath:'./public/js/',
	jsLibsPath:'./public/libs/',
	imgPath:'./public/imgs/',
	videoPath:'./public/music/',
	
	commonDestPath:'./dist/common',
	htmlDestPath:'./dist',
	cssDestPath:'./dist/css',
	jsDestPath:'./dist/js',
	jsLibsDestPath:'./dist/libs',
	imgDestPath:'./dist/imgs',
	videoDestPath:'./dist/music'
}

/*----------------------------- CSS ----------------------------*/
function css() {
	return src([configPath.cssPath+'*.less', configPath.cssPath+'*.scss', configPath.cssPath+'*.css'])
		// 转换 less
		.pipe(less())
		// 转换 scss
		.pipe(sass().on('error', sass.logError))
		.pipe(csso())
		.pipe(dest(configPath.cssDestPath))
		.pipe(connect.reload());
}
exports.css = css;

/*----------------------------- JS ----------------------------*/
function js() {
	return src(configPath.jsPath+'*.js')
		// ES6 语法转换
		.pipe(babel({
			presets: ['@babel/env']
		}))
		// 压缩 JS
		.pipe(uglify())
		.pipe(dest(configPath.jsDestPath))
		.pipe(connect.reload());
}
exports.js = js;

function copyLibs() {
	return src(configPath.jsLibsPath+'*.*')
		.pipe(dest(configPath.jsLibsDestPath))
		.pipe(connect.reload());
}
exports.copyLibs = copyLibs;

/*----------------------------- HTML ----------------------------*/
function html() {
	return src(configPath.htmlPath+'*.html')
		.pipe(fileinclude({
			prefix: '@@',
			basepath: '@file'
		}))
		// 压缩 HTML 去掉换行
		.pipe(htmlmin({
			collapseWhitespace: true
		}))
		.pipe(dest(configPath.htmlDestPath))
		.pipe(connect.reload());
}
exports.html = html;

function copyCommon() {
	return src(configPath.commonPath+'*.*')
		.pipe(dest(configPath.commonDestPath))
		.pipe(connect.reload());
}
exports.copyCommon = copyCommon;

/*----------------------------- img ----------------------------*/
function copyImg() {
	return src(configPath.imgPath+'*')
		.pipe(dest(configPath.imgDestPath))
		.pipe(connect.reload());
}
exports.copyImg = copyImg;

/*----------------------------- video ----------------------------*/

function copyVideo() {
	return src(configPath.videoPath+'*')
		.pipe(dest(configPath.videoDestPath))
		.pipe(connect.reload());
}
exports.copyVideo = copyVideo;

// 清理dist内所有文件  (防止仅覆盖不删除)
function clean() {
	return del([configPath.htmlDestPath+'/**']).then(() => {
		console.log('项目dist初始化清理完成...');
	})
}
exports.clean = clean;

// 开发模式 启动watch服务
function watchList() {
	watch(configPath.cssPath+'*.scss', series(css));
	watch(configPath.cssPath+'*.less', series(css));
	watch(configPath.cssPath+'*.css', series(css));
	watch(configPath.jsPath+'*.js', series(js));
	watch(configPath.jsLibsPath+'*', series(copyLibs));
	watch(configPath.commonPath+'*', series(copyCommon));
	watch(configPath.htmlPath+'*.html', series(html));
	watch(configPath.imgPath+'*', series(copyImg));
	watch(configPath.videoPath+'*', series(copyVideo));
}

// 服务器配置
let serverOptions = {
	name: 'development',
	root: configPath.htmlDestPath,
	host: '127.0.0.1',
	port: 8000,
	livereload: true
}

function init() {
	return [clean, js, copyLibs, copyCommon,html, css, copyImg, copyVideo];
}

function server() {
	connect.server(serverOptions);
}

//parallel 执行不固定项函数
exports.server = series(...init(), parallel(server, watchList));

// 默认 组合打包所有任务
exports.default = series(clean, js, copyLibs,copyCommon, html, css, copyImg, copyVideo);

/* 
	指令:
	gulp 打包
	gulp server 开启服务
 */